<?php 
require('../db/opendb.php');

$lquery = "select * from Listing_Images where ID = '".$_GET['pic_id']."'";
$lresult = mysql_query($lquery) or die ("SQL Error".mysql_error());
$row = mysql_fetch_assoc($lresult);

//echo $row['Main_Photo'];
$sql = "delete from Listing_Images where ID = '".$_GET['pic_id']."'";
$result = mysql_query($sql) or die ("SQL Error".mysql_error());

@unlink ("../listing_images/".$row['Path']);
@unlink ("../listing_images/resize_".$row['Path']);
@unlink ("../listing_images/t_".$row['Path']);
@unlink ("../listing_images/vt_".$row['Path']);

$cquery = "select * from Listing_Images where Listing_ID = '".$row['Listing_ID']."' and Main_Photo = 1";
$cresult = mysql_query($cquery) or die ("SQL Error".mysql_error());
//
	
if(!mysql_num_rows($cresult))
{
	$squery = "select * from Listing_Images where Listing_ID = '".$row['Listing_ID']."' limit 1";
	$sresult = mysql_query($squery) or die ("SQL Error".mysql_error());
	$srow = mysql_fetch_assoc($sresult);
	
	$usql = "update Listing_Images set Main_Photo = '1' where ID = '".$srow['ID']."'";
	$uresult = mysql_query($usql) or die ("SQL Error".mysql_error());
	//echo $usql;
}

echo "<meta http-equiv=refresh content=0;URL=edit_photos_listings.php?listing_id=".$_GET['listing_id'].">";

?>